package top.xiaoshuaila.moments.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import top.xiaoshuaila.moments.pojo.Dynamic;
import top.xiaoshuaila.moments.pojo.Zaner;

import java.util.List;

@Repository
public interface DetailMapper {
    @Select("select \n" +
            "  a.`id` id,\n" +
            "  a.`userid` userid,\n" +
            "  a.`time` time,\n" +
            "  a.`iszhuan` iszhuan,\n" +
            "  a.`dyid` dyid,\n" +
            "  a.`selfonly` selfonly,\n" +
            "  b.`text` text,\n" +
            "  b.`title` title,\n" +
            "  b.`image` image,\n" +
            "  b.`userid` dyuserid,\n" +
            "  c.num zan,\n" +
            "  d.zhuan zhuan,\n" +
            "  e.`realname` realname,\n" +
            "  f.realname username,\n" +
            "  f.headphoto headphoto,\n" +
            "  (select count(*)>0 from zan where zan.userid=${userid} and zan.dyid=a.id) iszan,\n" +
            " (a.iszhuan =false or " +
            "(select COUNT(*) from userdy where userdy.iszhuan=0 and userdy.dyid=a.dyid and userdy.selfonly=0)>0 or" +
            "(select COUNT(*) from userdy where userdy.iszhuan=0 and userdy.dyid=a.dyid and userdy.userid=${userid})>0) showdetail "+
            "from\n" +
            "  userdy a \n" +
            "  left join dynamic b \n" +
            "    on a.`dyid` = b.`id` \n" +
            "  left join \n" +
            "    (select \n" +
            "      dyid,\n" +
            "      count(*) as num \n" +
            "    from\n" +
            "      zan \n" +
            "    group by zan.`dyid`) c \n" +
            "    on a.id = c.dyid \n" +
            "  left join \n" +
            "    (select \n" +
            "      dyid,\n" +
            "      count(*)-1 as zhuan \n" +
            "    from\n" +
            "      userdy \n" +
            "    group by userdy.`dyid`) d \n" +
            "    on b.id = d.dyid \n" +
            "  left join \n" +
            "    users e \n" +
            "    on b.`userid` = e.`id` \n" +
            "  left join users f \n" +
            "    on a.userid = f.`id` \n" +
            "where a.id = ${dyid}  "
            )
    Dynamic getdetail(long dyid, long userid);
    @Select("select selfonly>0 from userdy where id=${dyid} and userid=${userid}")
    boolean isself(long dyid, long userid);

    @Update("update userdy set selfonly=0 where id=${dyid} and userid=${userid}")
    void removeself(long dyid, long userid);

    @Update("update userdy set selfonly=1 where id=${dyid} and userid=${userid}")
    void setself(long dyid, long userid);


    @Delete("delete zan from zan,userdy where zan.dyid=userdy.id and userdy.id=${dyid} and userdy.userid=${userid}")
    void deletezhuanzan(long dyid, long userid);
    @Delete("delete from userdy where userdy.id=${dyid} and userdy.userid=${userid}")
    void deletezhuandy(long dyid, long userid);

    @Delete("delete \n" +
            "  zan \n" +
            "from\n" +
            "  zan,\n" +
            "  userdy \n" +
            "where zan.dyid = userdy.id \n" +
            "  and userdy.dyid = \n" +
            "  (select \n" +
            "    userdy.dyid \n" +
            "  from\n" +
            "    userdy \n" +
            "  where userdy.id = ${dyid} \n" +
            "    and userdy.userid = ${userid})")
    void deletezan(long dyid, long userid);

    @Delete("delete dynamic \n" +
            "from\n" +
            "  dynamic,\n" +
            "  userdy \n" +
            "where dynamic.id = userdy.dyid \n" +
            "  and userdy.id = ${dyid}")
    void deletedy(long dyid, long userid);

    @Delete("delete \n" +
            "  a \n" +
            "from\n" +
            "  userdy a,\n" +
            "  (select \n" +
            "    userdy.dyid id \n" +
            "  from\n" +
            "    userdy \n" +
            "  where userdy.id = ${dyid} \n" +
            "    and userdy.userid = ${userid}) b \n" +
            "where a.dyid = b.id ;")
    void deletezhuan(long dyid, long userid);

    @Select("select \n" +
            "  b.id,\n" +
            "  b.realname,\n" +
            "  a.time,\n" +
            "  b.headphoto,\n" +
            "  b.introduction,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where focus.userid = b.id \n" +
            "    and focus.followerid = ${userid}) > 0 as isfocuto,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where focus.userid = ${userid} \n" +
            "    and focus.followerid = b.id) > 0 as isfan, \n" +
            "      ${userid}=b.id as isself \n" +

            "from\n" +
            "  zan a \n" +
            "  left join users b \n" +
            "    on a.userid = b.id \n" +
            "where a.dyid = ${dyid}  order by a.time desc limit ${page},${num}")
    List<Zaner> getzaner(long dyid, long userid,int page,int num);
}
